<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AdminLogin</title>
    <link href="css/AdminLogin.css"
          type="text/css" rel="stylesheet">
    <link href="bootstrap-4.6.1-dist/css/bootstrap.min.css"
          rel="stylesheet" type="text/css">

</head>
<body>
<div id="login-box">
    <div id="p"><h1>Admin Login</h1></div>
    <form id="LoginForm" >
        <div class="form-group row">
            <label class="col-form-label col-2 text-center">账号:</label>
            <div class="col-8">
                <input type="text" name="adminName" class="form-control"placeholder="账号">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-form-label col-2 text-center">密码:</label>
            <div class="col-8">
                <input type="password" name="adminPwd" class="form-control"placeholder="密码">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-form-label col-2 text-center">验证码:</label>
            <div class="col-8">
                <input type="text" name="checkCode" class="form-control" id="code" style="width: 60%;display: inline-block;margin-right: 5%;">
                <img src="validateCode" id="cImage" style="height: 35%;width: 30%;" alt="网络问题">
            </div>

        </div>
        <div class="form-group row">
            <label class="col-form-label col-2 text-center"></label>
            <div class="col-8">
                <button type="button" id="btnLogin" class="btn btn-lg btn-success">登录</button>
                <button type="reset" id="btnReg"class="btn btn-lg btn-danger">重置</button>
            </div>
        </div>
    </form>
    <script src="jquery/jquery-3.6.0.min.js"></script>
    <script>
        let checkCode='';
        $("#cImage").click(function (){
            this.src='validateCode?time'+new Date().getTime();
        });
        $.fn.serializeObject=function ($obj){
            let o={};
            let a=$obj.serializeArray();
            $.each(a,function (){
                if(o[this.name]){
                    if(!o[this.name].push){
                        o[this.name]=[o[this.name]];
                    }
                    o[this.name]=this.value||'';
                }else o[this.name]=this.value||'';
            });
            return o;
        }
        $("#btnLogin").click(function (){
           // if($("#code").val()!=checkCode){
           //     alert("验证码错误");
           //     $("#code").val("");
           //     return false;
           // }
            $.post('realCheckCode','code='+$("#code").val(),function (rst){
                if(rst){
                    let form=$("#LoginForm").serialize();
                    console.log(form);
                    $.post('admin/login',form,function (rst){
                        if(rst){
                            alert("管理员登录成功");
                            sessionStorage.setItem("adminLogin",JSON.stringify($.fn.serializeObject($("#LoginForm"))));
                            location.href="AdminDoCourse.html";
                        }
                        else{
                            alert("没有找到该管理员账号，或密码错误");
                            $("input").val("");
                        }
                    });
                }
                else{
                    alert("验证码不正确");
                    $("#code").val("");
                }
            });


        });

    </script>

</div>